package com.cloud.service.Impl;

import com.cloud.dao.UserNoticeDao;
import com.cloud.domain.LoginUser;
import com.cloud.myInterface.Factory;
import com.cloud.pojo.NoticeForUser;
import com.cloud.pojo.NoticeForUserVue;
import com.cloud.service.UserNoticeService;
import com.cloud.tools.CommonResult;
import com.cloud.tools.CommonResultFactory;
import com.cloud.utils.MyTime;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： linJianHong
 * @dateTime： 14:42 2022/4/24
 * @Description:
 * @ClassName:
 */
@Service
public class UserNoticeServiceImpl implements UserNoticeService {
    @Resource
    UserNoticeDao userNoticeDao;

    private final Factory resultFactory = new CommonResultFactory();

    @Override
    public CommonResult fromManager() {
        List<NoticeForUser> result1 = userNoticeDao.fromManager();
        List<NoticeForUserVue> result2 = new ArrayList<>();
        for(NoticeForUser item: result1){
            NoticeForUserVue noticeForUserVue = new NoticeForUserVue();
            noticeForUserVue.setNotice(item.getNotice());
            noticeForUserVue.setTitle(item.getTitle());
            noticeForUserVue.setPublishTime(MyTime.dateTime2String(item.getPublishTime()));
            result2.add(noticeForUserVue);
        }
        if(result2.size() >= 1){
            return resultFactory.getResultInstance(200,"公告查看成功",result2);
        }
        return resultFactory.getResultInstance(200,"暂无公告",null);
    }

    @Override
    public CommonResult fromApproval(String isVolunteer) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        int defaultRegion = loginUser.getUser().getDefaultRegion();
        List<NoticeForUser> result1 = userNoticeDao.fromApproval(defaultRegion, isVolunteer);
        List<NoticeForUserVue> result2 = new ArrayList<>();
        for(NoticeForUser item: result1){
            NoticeForUserVue noticeForUserVue = new NoticeForUserVue();
            noticeForUserVue.setNotice(item.getNotice());
            noticeForUserVue.setTitle(item.getTitle());
            noticeForUserVue.setPublishTime(MyTime.dateTime2String(item.getPublishTime()));
            result2.add(noticeForUserVue);
        }
        if(result2.size() >= 1){
            return resultFactory.getResultInstance(200,"公告查看成功",result2);
        }
        return resultFactory.getResultInstance(200,"暂无公告",null);
    }
}
